import { defineStore } from 'pinia'
import { constantRoutes } from '@/router'
import { getMyMenusTree } from '@/api/login'
import { filterAsyncRoutes } from './handle.js'
// 定义容器(导出) 参数列表：
// 参数一：容器名称，必须唯一；
// 参数二：选项对象
export const useMainStore = defineStore('main', {
    // 类似组件的data,存储全局状态
    state: () => {
        return {
            token: '',
            name: '',
            isCollapse: false,
            authBtnList: ['edit', 'delete'],
            sidebarRoutes: [],
            addRoutes: [
                {
                    path: "/system",
                    name: "system",
                    meta: { title: "系统管理", icon: "Tools", hidden: false },
                    component: "Layout",
                    children: [
                        {
                            path: "users",
                            name: "users",
                            meta: { title: "人员管理", icon: "", hidden: false, noCache: false },
                            component: "/system/users",
                            children: []
                        },
                        {
                            path: "roles",
                            name: "roles",
                            meta: { title: "角色管理", icon: "", hidden: false, noCache: false },
                            component: "/system/roles",
                            children: []
                        },
                        {
                            path: "menus",
                            name: "menus",
                            meta: { title: "菜单管理", icon: "", hidden: false, noCache: false },
                            component: "/system/menus",
                            children: []
                        }
                    ]
                },
                {
                    path: "/nest",
                    name: "nest",
                    meta: { title: "嵌套页面", icon: "Tools", hidden: false },
                    component: "Layout",
                    children: [
                        {
                            path: "nest-child1",
                            name: "nestChild1",
                            meta: { title: "二级页面1", icon: "", hidden: false },
                            component: "/nest/nest1",
                            children: [
                                {
                                    path: "nest-item",
                                    name: "nestItem",
                                    meta: { title: "三级页面1", icon: "", hidden: false, noCache: true },
                                    component: "/nest/nest1-1",
                                    children: []
                                }
                            ]
                        },
                        {
                            path: "nest-child2",
                            name: "nestChild2",
                            meta: { title: "二级页面2", icon: "", hidden: false, noCache: true },
                            component: "/nest/nest2",
                            children: []
                        },
                        {
                            path: "nest-child3",
                            name: "nestChild3",
                            meta: { title: "二级页面3", icon: "", hidden: false, noCache: true },
                            component: "/nest/nest3",
                            children: []
                        },
                        {
                            path: "nest-child4",
                            name: "nestChild4",
                            meta: { title: "二级页面4", icon: "", hidden: false, noCache: true },
                            component: "/nest/nest4",
                            children: []
                        },
                        {
                            path: "nest-child5",
                            name: "nestChild5",
                            meta: { title: "二级页面5", icon: "", hidden: false, noCache: true },
                            component: "/nest/nest5",
                            children: []
                        },
                        {
                            path: "nest-child6",
                            name: "nestChild6",
                            meta: { title: "二级页面6", icon: "", hidden: false, noCache: true },
                            component: "/nest/nest6",
                            children: []
                        },
                        {
                            path: "nest-child7",
                            name: "nestChild7",
                            meta: { title: "二级页面7", icon: "", hidden: false, noCache: true },
                            component: "/nest/nest7",
                            children: []
                        },
                        {
                            path: "nest-child8",
                            name: "nestChild8",
                            meta: { title: "二级页面8", icon: "", hidden: false, noCache: true },
                            component: "/nest/nest8",
                            children: []
                        },
                        {
                            path: "nest-child9",
                            name: "nestChild9",
                            meta: { title: "二级页面9", icon: "", hidden: false, noCache: true },
                            component: "/nest/nest9",
                            children: []
                        },
                        {
                            path: "nest-child10",
                            name: "nestChild10",
                            meta: { title: "二级页面10", icon: "", hidden: false, noCache: true },
                            component: "/nest/nest10",
                            children: []
                        },
                        {
                            path: "nest-child11",
                            name: "nestChild11",
                            meta: { title: "二级页面11", icon: "", hidden: false, noCache: true },
                            component: "/nest/nest11",
                            children: []
                        },
                        {
                            path: "nest-child12",
                            name: "nestChild12",
                            meta: { title: "二级页面12", icon: "", hidden: false, noCache: true },
                            component: "/nest/nest12",
                            children: []
                        }
                    ]
                },
                {
                    path: "/:catchAll(.*)",
                    redirect: '/404',
                    hidden: true
                },
            ]
        }
    },
    // 类似组件的computed，用来封装计算属性，有缓存的功能
    getters: {},
    // 类似组件的methods，用来封装操作逻辑，修改state
    actions: {
        // 生成路由
        GenerateRoutes() {
            return new Promise(resolve => {
                // getMyMenusTree()
                const res = this.addRoutes;
                const asyncRouter = filterAsyncRoutes(res)
                // 合并完整路由
                this.sidebarRoutes = constantRoutes.concat(asyncRouter)
                resolve(asyncRouter)
            })
        }
    },
    // 持久化
    persist: {
        key: 'storekey',
        storage: window.sessionStorage,
        paths: ['token']
    }
})
